package com.zl.csMall.basic.busEnum.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zl.csMall.code.config.token.TokenTriggerAspect;
import com.zl.csMall.code.constant.EntiretyConstant;
import com.zl.csMall.basic.busEnum.po.BusEnum;
import com.zl.csMall.basic.busEnum.service.BusEnumService;
import com.zl.csMall.util.Util;
import com.zl.csMall.vo.ReturnModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description: BusEnumController
 * @author: ChenNing
 * @date 2025-10-20 11:37
 */
@CrossOrigin(origins = "*")
@Api(tags = "枚举")
@RestController
@RequestMapping("csMall/basic/enum")
public class BusEnumController {

    @Autowired
    private BusEnumService busEnumService;


    /**
     * @description: 查询全部商品分类枚举
     * @author ChenNing
     * @date: 2025-10-20 11:51
     */
    @ApiOperation(value = "查询全部商品分类", response = BusEnum.class)
    @CrossOrigin("*")
    @GetMapping("type")
    public ReturnModel getTypeEnum(){
        LambdaQueryWrapper<BusEnum> busEnumQuery = new LambdaQueryWrapper<>();
        busEnumQuery.select(BusEnum::getId, BusEnum::getSort, BusEnum::getName)
                .eq(BusEnum::getGroupName, EntiretyConstant.BusEnumGroup.TYPE)
                .orderByAsc(BusEnum::getSort);
        List<Map<String, Object>> enumList = busEnumService.listMaps(busEnumQuery);
        return new ReturnModel(enumList);
    }

    /**
     * @description: 添加枚举
     * @author ChenNing
     * @date: 2025-10-20 11:39
     */
    @CrossOrigin("*")
    @ApiOperation(value = "添加枚举 - 商家")
    @TokenTriggerAspect
    @PostMapping
    public ReturnModel addBusEnum(@RequestHeader(required = false) String token,
                                  @Validated @RequestBody BusEnum busEnum){
        // 判断枚举名称是否重复
        boolean isRepeat = busEnumService.isRepeat(0, busEnum.getName());
        if (isRepeat){
            return new ReturnModel(false, "枚举名称重复！");
        }

        Date date = new Date();
        busEnum.setId(Util.getUuId());
        busEnum.setSort(busEnum.getSort() == null ? 0 : busEnum.getSort());
        busEnum.setCreateDate(date);
        busEnum.setUpdateDate(date);
        boolean result = busEnumService.save(busEnum);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "枚举添加失败！");
    }

    /**
     * @description: 修改枚举
     * @author ChenNing
     * @date: 2025-10-20 11:47
     */
    @CrossOrigin("*")
    @ApiOperation(value = "修改枚举 - 商家")
    @TokenTriggerAspect
    @PutMapping
    public ReturnModel updateBusEnum(@RequestHeader(required = false) String token,
                                     @RequestBody BusEnum busEnum){
        if (busEnum == null || busEnum.getId() == 0){
            return new ReturnModel(false, "枚举ID不能为空！");
        }
        // 判断枚举名称是否重复
        boolean isRepeat = busEnumService.isRepeat(busEnum.getId(), busEnum.getName());
        if (isRepeat){
            return new ReturnModel(false, "枚举名称重复！");
        }
        busEnum.setUpdateDate(new Date());
        boolean result = busEnumService.updateById(busEnum);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "枚举更新失败！");
    }

    /**
     * @description: 删除枚举
     * @param id 枚举ID
     * @author ChenNing
     * @date: 2025-10-20 13:42
     */
    @CrossOrigin("*")
    @ApiOperation(value = "根据ID删除枚举 - 商家")
    @TokenTriggerAspect
    @DeleteMapping
    public ReturnModel deleteBusEnum(@RequestHeader(required = false) String token,
                                     @RequestParam("id") Long id){
        if (Util.isNullOrEmpty(id)){
            return new ReturnModel(false, "枚举ID为空！");
        }
        boolean result = busEnumService.removeById(id);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "枚举删除失败！");
    }

}
